package com.sino.operation.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sino.operation.commons.response.CenterResponseEntity;
import com.sino.operation.commons.response.CenterResponseEnum;
import com.sino.operation.entity.SysTeller;
import com.sino.operation.service.SysTellerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("centerTellerManager")
@Api(value = "中心柜员管理",tags="中心柜员管理")
public class CenterTellerManagerController {
    @Autowired
    private SysTellerService sysTellerService;

    @GetMapping("/selectTellerList")
    @ApiOperation(value = "查询中心柜员列表")
    @ResponseBody
    public  IPage<SysTeller>  getTellerList(@RequestParam Map<String,String> map){
        IPage<SysTeller> sysTellerIPage = sysTellerService.selectList(map);
        return sysTellerIPage;
    }

    @PostMapping("/addTeller")
    @ApiOperation(value = "新增中心柜员")
    @ResponseBody
    public CenterResponseEntity addTeller(SysTeller sysTeller){
        try {
            sysTellerService.saveOrUpdate(sysTeller);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CenterResponseEntity.build();
    }

    @PostMapping("/deleteTeller")
    @ApiOperation(value = "批量删除中心柜员")
    @ResponseBody
    public CenterResponseEntity deleteTeller(List ids){
        try {
            sysTellerService.removeByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return CenterResponseEntity.buildError();
        }
        return CenterResponseEntity.build();
    }

    @GetMapping("/selectTellerById")
    @ApiOperation(value = "根据ID查询中心柜员")
    @ResponseBody
    public CenterResponseEntity selectTellerById(@RequestParam String id){
        SysTeller sysTeller=null;
        try {
             sysTeller = sysTellerService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return CenterResponseEntity.buildError();
        }
        return CenterResponseEntity.build(SysTeller.class).setData(sysTeller);
    }
    @PostMapping("/updateTeller")
    @ApiOperation(value = "修改中心柜员")
    @ResponseBody
    public CenterResponseEntity updateTeller(SysTeller sysTeller){
        try {
            sysTellerService.saveOrUpdate(sysTeller);
        } catch (Exception e) {
            e.printStackTrace();
            return CenterResponseEntity.buildError();
        }
        return CenterResponseEntity.build();
    }
    @PostMapping("/UpdatePassWord")
    @ApiOperation(value = "强制修改密码")
    @ResponseBody
    public CenterResponseEntity UpdatePassWord(SysTeller sysTeller){
        try {
            SysTeller teller = sysTellerService.selectTellerByTellerCode(sysTeller.getTellerCode());
            String tellerPassword = teller.getTellerPassword();//原密码

            if (sysTeller.getTellerPassword().equals(tellerPassword)){
                teller.setTellerPassword(sysTeller.getNewPassWord());//更改密码
            }else {
             return CenterResponseEntity.buildError().setCode(CenterResponseEnum.OLD_PASSWORD_ERROR.getCode()).setMsg("原密码错误");
            }
            if (!sysTeller.getNewPassWord().equals(sysTeller.getVerifyPassWord())){
                return CenterResponseEntity.buildError().setCode(CenterResponseEnum.TWO_PASSWORD_ERROR.getCode()).setMsg("两次密码不一致");
            }
            sysTellerService.saveOrUpdate(teller);
        } catch (Exception e) {
            e.printStackTrace();
            return CenterResponseEntity.buildError();
        }
        return CenterResponseEntity.build();
    }
    @PostMapping("/updateTellerById")
    @ApiOperation(value = "柜员强制签退/重置密码")
    @ResponseBody
    public CenterResponseEntity updateTellerById(@ApiParam("柜员id")String Id, @ApiParam("签退/重置标志(1-重置密码，2-强制签退)")String flag){
        try {
            SysTeller teller = sysTellerService.getById(Id);
            if (flag.equals("1")){
                teller.setTellerPassword("888888");
            }else {
                teller.setTellerState("2");
            }
            sysTellerService.saveOrUpdate(teller);
        } catch (Exception e) {
            e.printStackTrace();
            return CenterResponseEntity.buildError();
        }
        return CenterResponseEntity.build();
    }
}
